const express = require('express'),
    router = express.Router(),
    Api = require('../models/api.js'),
    apix = new Api();

router.get('/', (req, res, next) => {
    res.render('index',{title:"无虑Blog" });
});
router.get('/reg', (req, res, next) => {
    res.render('reg',{title:"注册" });
});
router.get('/blog', (req, res, next) => {
    let title = "无虑Blog | 博客首页",
        articleData,
    nav = req.query.nav;
    if(nav){
        apix.getArticleByTag(nav,result => {
            articleData = result;
            res.render('blog',{title:title ,articleData:articleData});
        })
    }else{
        apix.getArticle(result => {
            articleData = result;
            res.render('blog',{title:title ,articleData:articleData});
        })
    }
});
router.get('/logout',(req,res) => {
    res.clearCookie('islogin');
    req.session.destroy();
    res.redirect("/" );
});
module.exports = router;
